package com.example.servicetest;

/**  类  定义复数：加法 减法 乘法 取模 short转复数 */
public class ComplexCalculation
{
    ComplexCalculation()
    {
        super();
    }
    /**  复数加法  */
    private ComplexNumber ComplexAdd(ComplexNumber temp1,ComplexNumber temp2)
    {
        ComplexNumber result = new ComplexNumber();
        result.real = temp1.real + temp2.real;
        result.img = temp1.img + temp2.img;
        return result;
    }
    /**  复数减法  */
    private ComplexNumber ComplexMinus(ComplexNumber temp1,ComplexNumber temp2)
    {
        ComplexNumber result = new ComplexNumber();
        result.real = temp1.real - temp2.real;
        result.img = temp1.img - temp2.img;
        return result;
    }
    /**  复数乘法  */
    private ComplexNumber ComplexMultiply(ComplexNumber temp1,ComplexNumber temp2)
    {
        ComplexNumber result = new ComplexNumber();
        result.real = temp1.real * temp2.real - temp1.img * temp2.img;
        result.img = temp1.real * temp2.img + temp1.img * temp2.real;
        return result;
    }
    /**  取模  */
    private double ComplexMagnitudeDouble(ComplexNumber complexNumber)
    {
        return Math.hypot(complexNumber.real,complexNumber.img);
    }
    private short ComplexMagnitudeShort(ComplexNumber complexNumber)
    {
        double result = Math.hypot(complexNumber.real,complexNumber.img)/4;
        if (result<=0)
            result = 0;
        if (result>=32767)
            result = 32767;
        return (short) result;
    }
    /**  short[]扩展为ComplexNumber[]  */
    private ComplexNumber[] Short2Complex(short[] shorts)
    {
        int length = shorts.length;
        ComplexNumber[] result = new ComplexNumber[length];
        /**  需要对每个result初始化，不然会报空指针错误  */
        for (int i = 0;i<length;i++)
        {
            result[i] = new ComplexNumber();
            result[i].real = (double) shorts[i];
            result[i].img = (double) 0;
        }
        return result;
    }
}
